<template>
    <el-dialog :title="!dataForm.pkTemplate ? '新增' : '修改'" :close-on-click-modal="false" :before-close="closeDailog"
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="模板名称" prop="templateName">
                <el-input v-model="dataForm.templateName" placeholder="模板名称" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="模板内容" prop="templateContent">
                <!-- <el-input v-model="dataForm.templateContent" placeholder="模板内容" maxlength="30" show-word-limit>
                </el-input> -->

                <!-- <d2-quill style="min-height: 200px; margin-bottom: 20px;height: 600px;"
                    v-model="dataForm.templateContent" @text-change="textChangeHandler"
                    @selection-change="selectionChangeHandler" @editor-change="editorChangeHandler" /> -->

                <el-input type="textarea" :autosize="{ minRows: 10, maxRows: 20}" placeholder="模板内容" v-model="dataForm.templateContent">
                </el-input>
            </el-form-item>
            <el-form-item label="模板参考来源" prop="templateSource">
                <el-input v-model="dataForm.templateSource" placeholder="模板参考来源" maxlength="30" show-word-limit>
                </el-input>
            </el-form-item>
            <el-form-item label="模板描述" prop="templateDesc">
                <el-input v-model="dataForm.templateDesc" placeholder="模板描述" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <!-- <el-form-item label="模板版本" prop="version">
                <el-input v-model="dataForm.version" placeholder="模板版本" maxlength="30" show-word-limit></el-input>
            </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog()">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as codegeneratetemplateService from '@/api/development/codegeneratetemplate'

    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkTemplate: 0,
                    templateName: '',
                    templateContent: '',
                    templateSource: '',
                    templateDesc: '',
                    version: '',
                    revision: '',
                    createdBy: '',
                    createdTime: '',
                    updatedBy: '',
                    updatedTime: '',
                    deleFlag: '',
                    pkGroup: '',
                    pkOrg: ''
                },
                dataRule: {
                    templateName: [{
                        required: true,
                        message: '模板名称不能为空',
                        trigger: 'blur'
                    }],
                    templateContent: [{
                        required: true,
                        message: '模板内容不能为空',
                        trigger: 'blur'
                    }],
                    templateSource: [{
                        required: true,
                        message: '模板参考来源不能为空',
                        trigger: 'blur'
                    }],
                    templateDesc: [{
                        required: true,
                        message: '模板描述不能为空',
                        trigger: 'blur'
                    }],
                    version: [{
                        required: true,
                        message: '模板版本不能为空',
                        trigger: 'blur'
                    }]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkTemplate = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkTemplate) {
                        codegeneratetemplateService.getCodeGenerateTemplateInfo(this.dataForm.pkTemplate).then(
                            data => {
                                this.dataForm.templateName = data.templateName
                                this.dataForm.templateContent = data.templateContent
                                this.dataForm.templateSource = data.templateSource
                                this.dataForm.templateDesc = data.templateDesc
                                this.dataForm.version = data.version
                            })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkTemplate': this.dataForm.pkTemplate || undefined,
                            'templateName': this.dataForm.templateName,
                            'templateContent': this.dataForm.templateContent,
                            'templateSource': this.dataForm.templateSource,
                            'templateDesc': this.dataForm.templateDesc,
                            'version': this.dataForm.version
                        }
                        var tick = !this.dataForm.pkTemplate ? codegeneratetemplateService
                            .addCodeGenerateTemplate(params) : codegeneratetemplateService
                            .updateCodeGenerateTemplate(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
            textChangeHandler(delta, oldDelta, source) {
                // console.group('QuillEditor textChangeHandler')
                console.log(delta, oldDelta, source)
                // console.groupEnd()
            },
            selectionChangeHandler(range, oldRange, source) {
                // console.group('QuillEditor selectionChangeHandler')
                // console.log(range, oldRange, source)
                // console.groupEnd()
            },
            editorChangeHandler(eventName, ...args) {
                // console.group('QuillEditor editorChangeHandler')
                // console.log(eventName, args)
                // console.groupEnd()
            }
        }
    }
</script>